package com.powerbosframework.beans;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;

import com.powerbosframework.beans.BeanObjectLoader;
import com.powerbosframework.beans.entity.BeanEntity;
import com.powerbosframework.beans.entity.ViewEntity;
import com.powerbosframework.context.ApplicatioinContextException;
import com.powerbosframework.context.resource.Resource;
import com.powerbosframework.context.resource.ResourceImpl;
import com.powerbosframework.util.ClassLoaderUtil;
import com.powerbosframework.util.StringUtil;
import com.powerbosframework.util.XMLUtil;

/**
 * POJO对象工厂类
 * 
 * @author youfu.wang
 * @version 1.0
 */
public class BeanObjectFactory {
	private static Map<String, ViewEntity> viewEntities = Collections
			.synchronizedMap(new HashMap<String, ViewEntity>());
	private static Map<String, BeanEntity> beanEntities = Collections
			.synchronizedMap(new HashMap<String, BeanEntity>());
	private static Map<String, Object> beanObjects = Collections
			.synchronizedMap(new HashMap<String, Object>());
	private static BeanObjectFactory instance = null;
	private XMLUtil xmlUtil = new XMLUtil();
	private BeanObjectLoader beanObjectLoader = new BeanObjectLoader(this);
	
	public BeanObjectFactory() {

	}

	public void clear() {
		if (beanEntities != null)
			beanEntities.clear();
		if (beanObjects != null)
			beanObjects.clear();
	}

	public void loadBeanObject(String location) throws IOException,
			ApplicatioinContextException {
		
		Resource resource = new ResourceImpl(location);
		InputStream stream = resource.getInputStream();
		if (stream == null)
			throw new IOException(resource + "资源文件没有找到");

		Document doc = xmlUtil.parse(stream);
		if (doc == null)
			throw new ApplicatioinContextException(resource + "资源文件解析失败");
		Element rootE = doc.getRootElement();
		if (rootE == null)
			throw new ApplicatioinContextException(resource + "资源文件解析失败");
		List includeInputStreamResources = null;
		includeInputStreamResources = rootE.getChildren("include");
		if (includeInputStreamResources == null)
			return;

		for (int i = 0; i < includeInputStreamResources.size(); i++) {
			Element resourceE = (Element) includeInputStreamResources.get(i);
			beanObjectLoader.loadBeanEntity(StringUtil.replaceNull(resourceE
					.getAttributeValue("resource")));
		}
		beanObjectLoader.loadBeanEntity(resource);
		// 加载POJO属性值
		beanObjectLoader.loadBeanPropertyValues();

	}

	public void setGlobalViews(String name, ViewEntity viewEntity) {
		viewEntities.put(name, viewEntity);
	}

	public ViewEntity getGlobalView(String name) {
		if (viewEntities.containsKey(name))
			return viewEntities.get(name);
		return null;
	}

	public void setBeanEntity(String id, BeanEntity beanEntity) {
		beanEntities.put(id, beanEntity);
	}

	public Map<String, BeanEntity> getBeanEntities() {
		return beanEntities;
	}

	public BeanEntity getBeanEntity(String id) {
		if (beanEntities.containsKey(id)) {
			return beanEntities.get(id);
		}
		return null;
	}

	public void setBean(String id, Object obj) {
		beanObjects.put(id, obj);
	}

	public Object getBean(String id) {
		if(beanEntities.containsKey(id)){
			BeanEntity beanEntity=beanEntities.get(id);			
			if("true".equals(beanEntity.getSingleton())){
				if (beanObjects.containsKey(id)) {
					return beanObjects.get(id);
				}
			}else{
				// 加载POJO属性值
				Object obj=beanObjectLoader.newBeanInstance(beanEntity);
				beanObjectLoader.loadBeanPropertyValues(beanEntity, obj);
				return obj;
			}
		}			
		return null;
	}
}
